<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $phone = $request->get('phone');
        $password = $request->get('password');

        $rules = [
            'phone' => 'required',
            'password' => 'required'
        ];

        $validator = Validator::make($request->all(), $rules);

        if($validator->fails()) {
            return $this->failed("登录失败！账号或密码错误！", 40100);
        }

        $user = $this->authService->login($phone, $password);
        if(!$user) {
            return $this->failed("登录失败！");
        }
        return $user;
    }

    public  function logout(Request $request)
    {
        $user = $request->user();
        $user->token = "";
        $user->save();

        return $this->success("logout success");
    }
}
